import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
export const ZRCMS_SITE_ID = 'zrcms_site_id'
export const MANUAL_CHANGE_SITE = 'manual_change_site'
export const getSiteId = (): number | null => {
  const siteId = wsCache.get(ZRCMS_SITE_ID)
  if (siteId != null) {
    return Number(siteId)
  }
  return 1
}
export const setSiteId = (siteId: number): void => {
  wsCache.set(ZRCMS_SITE_ID, String(siteId))
}
export const setManualChangeSite = (siteId: number): void => {
  wsCache.set(MANUAL_CHANGE_SITE, String(siteId))
}
export const getManualChangeSite = (): number | null => {
  const siteId = wsCache.get(MANUAL_CHANGE_SITE)
  if (siteId != null) {
    return Number(siteId)
  }
  return null
}
export const removeSiteId = (): void => {
  wsCache.delete(ZRCMS_SITE_ID)
}
export const getSiteHeader = (): any => {
  const siteId = getSiteId()
  return siteId != null ? siteId : 1
}
export const getRandomLower = (len, charSet) => {
  charSet = charSet || 'abcdefghijklmnopqrstuvwxyz'
  let randomString = ''
  for (let i = 0; i < len; i++) {
    const randomPoz = Math.floor(Math.random() * charSet.length)
    randomString += charSet.substring(randomPoz, randomPoz + 1)
  }
  return randomString
}
export const getRandomUpper = (len, charSet) => {
  charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  let randomString = ''
  for (let i = 0; i < len; i++) {
    const randomPoz = Math.floor(Math.random() * charSet.length)
    randomString += charSet.substring(randomPoz, randomPoz + 1)
  }
  return randomString
}
export const getRandomNumber = (len) => {
  const charSet = '0123456789'
  let randomString = ''
  for (let i = 0; i < len; i++) {
    const randomPoz = Math.floor(Math.random() * charSet.length)
    randomString += charSet.substring(randomPoz, randomPoz + 1)
  }
  return randomString
}
export const getRandomCharacter = () => {
  const characters = '!@#$%^&*(){}[]=<>/,.'
  return characters[Math.floor(Math.random() * characters.length)]
}
export function arr2obj(arr: any[]): Record<string, any> {
  const obj: Record<string, any> = {}
  arr.forEach((item: any) => {
    obj[item.code] = item
  })
  return obj
}

export const formatDuration = (duration?: number): string => {
  if (duration == null) {
    return ''
  }
  const hours = Math.floor(duration / 3600)
  const minutes = Math.floor((duration - hours * 3600) / 60)
  const seconds = duration - hours * 3600 - minutes * 60
  let str = ''
  if (hours > 0) {
    str = hours + ':'
  }
  if (minutes < 10) {
    str += '0' + minutes + ':'
  } else {
    str += minutes + ':'
  }
  if (seconds < 10) {
    str += '0' + seconds
  } else {
    str += seconds
  }
  return str
}

export function getModelData(): any {
  return {
    content: {
      mainFields: [
        { code: 'title', must: true, show: true, double: false, required: true },
        { code: 'subtitle', must: false, show: false, double: false, required: false },
        { code: 'outLink', must: false, show: true, double: false, required: false },
        { code: 'tagsNames', must: false, show: false, double: false, required: false },
        { code: 'keywords', must: false, show: true, double: false, required: false },
        { code: 'description', must: false, show: true, double: false, required: false },
        { code: 'author', must: false, show: false, double: true, required: false },
        {
          code: 'sysImage',
          must: false,
          show: true,
          double: false,
          required: false,
          type: 'image',
          imageWidth: 300,
          imageHeight: 200,
          imageMode: 'manual'
        },
        { code: 'sysAttach', must: false, show: false, double: false, required: false },
        { code: 'sysVideo', must: false, show: false, double: false, required: false },
        { code: 'sysAudio', must: false, show: false, double: false, required: false },
        { code: 'docPdf', must: false, show: false, double: false, required: false },
        {
          code: 'images',
          must: false,
          show: false,
          double: false,
          required: false,
          type: 'imageList',
          imageListType: 'pictureCard',
          imageMaxWidth: 1920,
          imageMaxHeight: 1920
        },
        { code: 'attachs', must: false, show: false, double: false, required: false },
        {
          code: 'text',
          must: false,
          show: true,
          double: false,
          required: true,
          type: 'editor',
          // editorType 预留后续接入其他编辑器
          editorType: 1,
          editorSwitch: false
        }
      ],
      rightFields: [
        { code: 'channel', must: true, show: true, required: true },
        { code: 'releaseTime', must: true, show: true, required: true },
        { code: 'contentMark', must: false, show: true, required: false },
        { code: 'block', must: false, show: false, required: false },
        { code: 'topLevel', must: false, show: true, required: true },
        { code: 'source', must: false, show: true, required: false },
        { code: 'subChannelIds', must: false, show: false, required: false },
        { code: 'topStartTime', must: false, show: false, required: false },
        { code: 'topEndTime', must: false, show: false, required: false },
        { code: 'offlineTime', must: false, show: false, required: false },
        { code: 'onlineTime', must: false, show: false, required: false },
        { code: 'pcTpl', must: false, show: false, required: false },
        { code: 'h5Tpl', must: false, show: false, required: false }
      ]
    },
    channel: {
      mainFields: [
        { code: 'name', must: true, show: true, double: true, required: true },
        { code: 'path', must: true, show: true, double: true, required: true },
        { code: 'outLinkUrl', must: false, show: false, double: false, required: true },
        { code: 'seoTitle', must: false, show: true, double: true, required: false },
        { code: 'seoKeyword', must: false, show: true, double: true, required: false },
        { code: 'seoDescription', must: false, show: true, double: false, required: false },
        { code: 'tplPc', must: false, show: true, double: true, required: false },
        { code: 'tplMobile', must: false, show: false, double: true, required: false },
        { code: 'contentTplPc', must: false, show: true, double: true, required: false },
        { code: 'contentTplMobile', must: false, show: false, double: true, required: false },
        { code: 'model', must: true, show: true, double: true, required: true },
        { code: 'contentModel', must: true, show: true, double: true, required: true },
        { code: 'commentStatus', must: false, show: true, double: true, required: true },
        { code: 'contributeStatus', must: false, show: true, double: true, required: true },
        { code: 'searchStatus', must: false, show: true, double: true, required: true },
        {
          code: 'listImage',
          must: false,
          show: true,
          double: false,
          required: false,
          type: 'image',
          imageWidth: 300,
          imageHeight: 200,
          imageMode: 'manual'
        },
        { code: 'txt', must: false, show: false, double: false, required: false }
      ],
      rightFields: [
        { code: 'parent', must: true, show: true, required: false },
        { code: 'processDefinitionId', must: false, show: true, required: false },
        { code: 'listPage', must: false, show: true, required: false },
        { code: 'pageSize', must: false, show: true, required: false },
        { code: 'listStatus', must: false, show: true, required: false },
        { code: 'sort', must: false, show: true, required: false }
      ]
    }
  }
}
export function getItemLabel(type: string, item: any) {
  const withLabelItems = ['path', 'listStatus', 'listPage']
  if (withLabelItems.includes(item.code)) {
    return `cms.${type}.${item.code}.label`
  }
  return `cms.${type}.${item.code}`
}
export function mergeModelFields(
  defaultFields: any[],
  modelVal: string | null | undefined,
  type: string
): any[] {
  const fields = JSON.parse(modelVal || '[]')
  const defaults = defaultFields.map((item: any) => ({
    ...item,
    label: getItemLabel(type, item)
  }))

  // 去除默认字段中不存在的字段
  fields.filter((field: any) => defaults.findIndex((item) => item.code === field.code) !== -1)
  defaults.forEach((item) => {
    const index = fields.findIndex((it: any) => it.code === item.code)
    if (index !== -1) {
      // 加上缺失属性，覆盖不可改属性
      fields[index] = {
        ...item,
        ...fields[index],
        must: item.must,
        label: item.label,
        type: item.type
      }
    } else {
      // 加上没有的字段
      fields.push(item)
    }
  })
  return fields
}
